package com.bigwolf.auth.controller;

import com.bigwolf.auth.form.LoginBody;
import com.bigwolf.auth.service.SysLoginService;
import com.bigwolf.common.core.constant.Constants;
import com.bigwolf.common.core.domain.R;
import com.bigwolf.common.core.utils.AjaxResult;
import com.bigwolf.common.security.service.TokenService;
import com.bigwolf.system.api.model.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class TokenController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody form) {
        logger.info("======form========"+form.getUsername()+":"+form.getPassword());
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        AjaxResult ajax = AjaxResult.success();
        ajax.put(Constants.TOKEN, tokenService.createToken(userInfo));
        return ajax;
    }

    @PostMapping("/test")
    public AjaxResult test() {
        return AjaxResult.success("ok");
    }
}
